/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 *
 * @version 1.0
 */
package shop.controller.front.wallet;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.services.front.record.RecordService;
import shop.services.front.record.bean.Record;
import shop.services.front.wallet.WalletService;
import shop.services.front.wallet.bean.Wallet;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * @类名称：WalletController
 * @创建人：Wzl
 * @创建时间：2017-09-20 下午15:55:25      
 * @版本号：1.0
 * @描述：用户提现申请
 */
@Controller
@RequestMapping("/front/wallet/")
public class WalletController extends FrontBaseController<Wallet> {
    private final  static  String RECORD_TRANSTYPE_CASH="1";
    private final  static  String WALLET_STATUS_INIT="1";
    @Resource
    private WalletService walletService;
    @Resource
    private RecordService recordService;
    @Resource
    private UserService userService;

    @Override
    public Services<Wallet> getService() {
        return walletService;
    }


    /**
     * 发起提现
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertWallet", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertWallet(HttpServletRequest request, Wallet wallet) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) request.getSession().getAttribute("userInfo");
        wallet.setUserID(user.getId());
        wallet.setUserName(user.getNickname());
        wallet.setStatus(WALLET_STATUS_INIT);
        if (user.getIncome().compareTo(wallet.getMoney()) != -1) {
            int transID = walletService.insert(wallet);//插入提现表
            user.setIncome(user.getIncome().subtract(wallet.getMoney()));
            userService.update(user);//更新用户钱包
            Record record = new Record();
            record.setUserID(user.getId());
            record.setTransType(RECORD_TRANSTYPE_CASH);
            record.setTransID(String.valueOf(transID));
            record.setMoney(wallet.getMoney());
            recordService.insert(record);//插入交易记录表
            return jsonResult;
        } else {
            jsonResult.setMessage("您的可提现金额不足，请重新输入提现金额！");
            jsonResult.setSuccess(false);
            return jsonResult;
        }
    }

    @Override
    public JSONResult deleteJson(HttpServletRequest request, Wallet wallet) throws Exception {
        return null;
    }
}
